// pages/sub_user/index.js
import {Model} from './model.js';
var model=new Model();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    editShow:false,
    editUser:{},
    newUsername:null,
    asyncClose:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getSubUser()
  },

  getSubUser(){
    model.getSubUser((res)=>{
      this.setData({
        list:res.data
      });
    });
  },
  onRemarkName(event){
    let editUser={
      uid:event.currentTarget.dataset.uid,
      username: event.currentTarget.dataset.username 
    }
    this.setData({
      editShow:true,
      editUser
    })
  },
  onClose() {
    this.setData({ 
      editShow: false,
      newUsername:null
    });
  },
  onEditUsername(event){
    let username=event.detail
    this.setData({
      newUsername:username
    })
  },
  onSubmit(){
    let data={}
    let newUsername=this.data.newUsername
  
    if(!newUsername){
      wx.showToast({
        title: '请输入备注名~',
        icon:'none'
      });
      return false;
    }
    data.uid=this.data.editUser.uid
    data.newUsername=newUsername
    wx.showLoading({
      title: "加载中...",
      mask: true,
    });
      
    model.editUserName(data,(res)=>{
      wx.hideLoading()
      if(res.data.code){
        wx.showToast({
          title: '修改成功',
          icon: 'none',
        });
        this.onClose()
        this.getSubUser()
      }else{
        wx.showToast({
          title: '修改失败，请稍后重试~',
          icon: 'none',
        });
      }
    },(res)=>{
      wx.hideLoading()
      this.onClose()
      wx.showToast({
        title: '修改失败，请稍后重试~',
        icon: 'none',
      });
    })
  }

})